/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.TextView;
import dk.redia.libraryapp.R;
import dk.redia.libraryapp.model.Library;

public class LibraryAdapter implements ListAdapter {

	private List<Library> libraries;
	private DataSetObserverCollection observers;
	private Context context;
	
	public LibraryAdapter(Context c) {
		libraries = new ArrayList<Library>();
		observers = new DataSetObserverCollection();
		this.context = c;
	}
	
	public int getCount() {
		return libraries.size();
	}

	public Object getItem(int arg0) {
		return libraries.get(arg0);
	}

	public long getItemId(int arg0) {
		return arg0;
	}

	public int getItemViewType(int arg0) {
		return IGNORE_ITEM_VIEW_TYPE;
	}

	public View getView(int position, View oldView, ViewGroup root) {
		View v;
		if (oldView==null) {
			LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.libraryopeninghourslistitem, null);
		} else {
			v = oldView;
		}

		Library library = libraries.get(position);
		TextView libraryNameTextView = (TextView)v.findViewById(R.id.openingHoursListItemLibraryNameTextView);
		TextView libraryOpenTextView = (TextView)v.findViewById(R.id.openingHoursListItemOpenTextView);
		
		v.setTag(library); // Bring the library object back with us when we are clicked
		libraryNameTextView.setText(library.getName());
		libraryOpenTextView.setVisibility(library.isOpen() ? View.VISIBLE : View.INVISIBLE);
		
		return v;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return false;
	}

	public boolean isEmpty() {
		return libraries.isEmpty();
	}

	public void registerDataSetObserver(DataSetObserver arg0) {
		observers.registerObserver(arg0);
	}

	public void unregisterDataSetObserver(DataSetObserver arg0) {
		observers.unregisterObserver(arg0);
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int arg0) {
		if (arg0>=this.libraries.size()) {
			throw new ArrayIndexOutOfBoundsException();
		} else {
			return true;
		}
	}
	
	public void clear() {
		this.libraries.clear();
		this.observers.notifyObservers();
	}
	
	public void addAll(List<Library> l) {
		libraries.addAll(l);
		observers.notifyObservers();
	}

}
